package com.controller.produce.server;

import com.bean.produce.ServerBean;
import com.dms.producer.DmsProducer;
import com.service.produce.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping("/server")
public class ProduceServerController {

    @Autowired
    private ServerService serverService;

    @RequestMapping("/produce")
    @ResponseBody
    public String producerServer() throws IOException {
        ServerBean serverBean = new ServerBean();
        serverService.insertServerBean(serverBean);
        DmsProducer<String, String> producer = new DmsProducer<String, String>();
        StringBuilder sb = new StringBuilder();
        try {
            for (int i = 0; i < 5; i++) {
                String data = "The server is " + serverBean + i;
                producer.produce("topic-Server", data);
                System.out.println("produce server:" + data);
                sb.append("\nproduce server:").append(data);
            }
        } catch (Exception e) {
            return e.getMessage();
        } finally {
            producer.close();
        }
        return sb.toString();
    }
}
